/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmSatuanBesar.java
 *
 * Created on Jan 24, 2012, 3:06:59 PM
 */
package SIMRS.Apotek.view;

import SIMRS.Apotek.sql.KoneksiDatabase;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import usu.widget.Form;

/**
 *
 * @author yunniie
 */
public class frmSatuanBesar extends Form {
    private DefaultTableModel model;
    

    /** Creates new form frmSatuanBesar */
    public frmSatuanBesar() {
        initComponents();
        model=new DefaultTableModel();
        tblSatuanBesar.setModel(model);
        model.addColumn("Kode Satuan Besar");
        model.addColumn("Satuan Besar");
        loadDataSatuanBesar();
        
        txtKodeSatuanBesar.setEnabled(false);
        txtSatuanBesar.setEnabled(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlForm1 = new SIMRS.Apotek.widget.PnlForm();
        label1 = new SIMRS.Apotek.widget.Label();
        txtKodeSatuanBesar = new SIMRS.Apotek.widget.TextBox();
        txtSatuanBesar = new SIMRS.Apotek.widget.TextBox();
        label3 = new SIMRS.Apotek.widget.Label();
        scrollPane1 = new SIMRS.Apotek.widget.ScrollPane();
        tblSatuanBesar = new SIMRS.Apotek.widget.Table();
        btnSimpan = new SIMRS.Apotek.widget.button2();
        btnUbah = new SIMRS.Apotek.widget.button2();
        btnHapus = new SIMRS.Apotek.widget.button2();
        btnTutup = new SIMRS.Apotek.widget.button2();
        label16 = new SIMRS.Apotek.widget.Label();

        setTitle(".: Form Satuan Besar");
        setAnimationHide(usu.widget.constan.Animation.HIDE_TO_RIGHT);
        setAnimationShow(usu.widget.constan.Animation.SHOW_FROM_LEFT);
        setResizable(false);
        setUndecorated(true);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        pnlForm1.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(57, 190, 53), 3, true));
        pnlForm1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        label1.setText("Kode Satuan Besar");
        pnlForm1.add(label1, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 130, 110, 20));
        pnlForm1.add(txtKodeSatuanBesar, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 130, 90, -1));
        pnlForm1.add(txtSatuanBesar, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 160, 170, -1));

        label3.setText("Satuan Besar");
        pnlForm1.add(label3, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 160, 100, 20));

        tblSatuanBesar.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tblSatuanBesar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblSatuanBesarMouseClicked(evt);
            }
        });
        scrollPane1.setViewportView(tblSatuanBesar);

        pnlForm1.add(scrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 190, 440, 180));

        btnSimpan.setIcon(new javax.swing.ImageIcon(getClass().getResource("/SIMRS/Apotek/gambar/bt_simpan.png"))); // NOI18N
        btnSimpan.setText("Tambah");
        btnSimpan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSimpanActionPerformed(evt);
            }
        });
        pnlForm1.add(btnSimpan, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 390, -1, -1));

        btnUbah.setIcon(new javax.swing.ImageIcon(getClass().getResource("/SIMRS/Apotek/gambar/bt_edit.png"))); // NOI18N
        btnUbah.setText("Ubah");
        btnUbah.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUbahActionPerformed(evt);
            }
        });
        pnlForm1.add(btnUbah, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 390, -1, -1));

        btnHapus.setIcon(new javax.swing.ImageIcon(getClass().getResource("/SIMRS/Apotek/gambar/bt_hapus.png"))); // NOI18N
        btnHapus.setText("Hapus");
        btnHapus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnHapusActionPerformed(evt);
            }
        });
        pnlForm1.add(btnHapus, new org.netbeans.lib.awtextra.AbsoluteConstraints(270, 390, -1, -1));

        btnTutup.setIcon(new javax.swing.ImageIcon(getClass().getResource("/SIMRS/Apotek/gambar/bt_close.png"))); // NOI18N
        btnTutup.setText("Tutup");
        btnTutup.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTutupActionPerformed(evt);
            }
        });
        pnlForm1.add(btnTutup, new org.netbeans.lib.awtextra.AbsoluteConstraints(370, 390, -1, -1));

        label16.setForeground(new java.awt.Color(255, 255, 255));
        label16.setText("Data Satuan Besar");
        label16.setFont(new java.awt.Font("Tahoma", 1, 18));
        pnlForm1.add(label16, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 40, 400, -1));

        getContentPane().add(pnlForm1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 500, 470));

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-495)/2, (screenSize.height-469)/2, 495, 469);
    }// </editor-fold>//GEN-END:initComponents

    private void btnTutupActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTutupActionPerformed
        dispose();
}//GEN-LAST:event_btnTutupActionPerformed

private void tblSatuanBesarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblSatuanBesarMouseClicked
// TODO add your handling code here:
    int i = tblSatuanBesar.getSelectedRow();
      
        txtKodeSatuanBesar.setText(model.getValueAt(i, 0).toString());
        txtSatuanBesar.setText(model.getValueAt(i, 1).toString());
        
}//GEN-LAST:event_tblSatuanBesarMouseClicked

private void btnSimpanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSimpanActionPerformed
// TODO add your handling code here:
    if(btnSimpan.getLabel().equals("Tambah")){
            
            btnSimpan.setLabel("Simpan");
            txtKodeSatuanBesar.setEnabled(true);
            txtSatuanBesar.setEnabled(true);
            tblSatuanBesarMouseClicked(null);
        } else if(btnSimpan.getLabel().equals("Simpan")){

    Connection c= KoneksiDatabase.getKoneksi(); //coding simpan
    String sql = "INSERT INTO satuanbesar (KdSatuanBesar, SatuanBesar) VALUES ( ?, ?)";
        try {
            PreparedStatement statement= c.prepareStatement(sql);
            statement.setString(1, txtKodeSatuanBesar.getText()); //sesuai tipe data di database
            statement.setString(2, txtSatuanBesar.getText());
            
            statement.executeUpdate();
            loadDataSatuanBesar();  //langsung tampil di tabel datanya
            
            JOptionPane.showMessageDialog(this, "Data Berhasil Disimpan");
            btnSimpan.setLabel("Tambah");
            txtKodeSatuanBesar.setEnabled(false);
            txtSatuanBesar.setEnabled(false);
            txtKodeSatuanBesar.setText("");
            txtSatuanBesar.setText("");
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(this, "Data gagal disimpan");
            
        }
        }
}//GEN-LAST:event_btnSimpanActionPerformed

private void btnUbahActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUbahActionPerformed
// TODO add your handling code here:
    if(btnUbah.getLabel().equals("Ubah")){
            
        txtKodeSatuanBesar.setEnabled(true);
        txtSatuanBesar.setEnabled(true);
            
            btnUbah.setLabel("Simpan");
            tblSatuanBesarMouseClicked(null);
        } else if(btnUbah.getLabel().equals("Simpan")){
 
    Connection c= KoneksiDatabase.getKoneksi(); //coding ubah
    String sql = "UPDATE satuanbesar SET SatuanBesar = ? WHERE KdSatuanBesar = '"+txtKodeSatuanBesar.getText()+"' ";
        try {
            PreparedStatement statement= c.prepareStatement(sql);
            
            statement.setString(1, txtSatuanBesar.getText());
            statement.executeUpdate();
            JOptionPane.showMessageDialog(this, "Data Berhasil Diubah");
            
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(this, "Data gagal diubah");
          //  ex.printStackTrace();
            btnUbah.setLabel("Ubah");
            txtKodeSatuanBesar.setEnabled(false);
            txtSatuanBesar.setEnabled(false);
            txtKodeSatuanBesar.setText("");
            txtSatuanBesar.setText("");
        } finally {
                btnUbah.setLabel("Ubah");
                loadDataSatuanBesar();
                tblSatuanBesarMouseClicked(null);
            

        }
        }
}//GEN-LAST:event_btnUbahActionPerformed

private void btnHapusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnHapusActionPerformed
// TODO add your handling code here:
    try {
        
            Connection c = KoneksiDatabase.getKoneksi();
            String sql = "DELETE FROM satuanbesar WHERE KdSatuanBesar = '"+txtKodeSatuanBesar.getText()+"' ";
            PreparedStatement p = c.prepareStatement(sql);
         
            p.executeUpdate();
            p.close();
            JOptionPane.showMessageDialog(this, "Data Berhasil di hapus!!!");
            txtKodeSatuanBesar.setText("");
            txtSatuanBesar.setText("");
    } catch (Exception e) {
        System.out.println("Terjadi error pada saat dihapus");
        } finally {
            loadDataSatuanBesar();
        
    }
}//GEN-LAST:event_btnHapusActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private SIMRS.Apotek.widget.button2 btnHapus;
    private SIMRS.Apotek.widget.button2 btnSimpan;
    private SIMRS.Apotek.widget.button2 btnTutup;
    private SIMRS.Apotek.widget.button2 btnUbah;
    private SIMRS.Apotek.widget.Label label1;
    private SIMRS.Apotek.widget.Label label16;
    private SIMRS.Apotek.widget.Label label3;
    private SIMRS.Apotek.widget.PnlForm pnlForm1;
    private SIMRS.Apotek.widget.ScrollPane scrollPane1;
    private SIMRS.Apotek.widget.Table tblSatuanBesar;
    private SIMRS.Apotek.widget.TextBox txtKodeSatuanBesar;
    private SIMRS.Apotek.widget.TextBox txtSatuanBesar;
    // End of variables declaration//GEN-END:variables

    private void loadDataSatuanBesar() {
        model.getDataVector().removeAllElements();
        model.fireTableDataChanged();
        Connection c= KoneksiDatabase.getKoneksi();
        try {
            Statement s = c.createStatement();
            String sql = "SELECT * FROM satuanbesar";
            ResultSet rs= s.executeQuery(sql);
            
            while (rs.next()){
                Object[] o = new Object[2];
                o[0]=rs.getString("KdSatuanBesar");
                o[1]=rs.getString("SatuanBesar");
                
                model.addRow(o);
             }
            rs.close();
            s.close();
        } catch (SQLException ex) {
            Logger.getLogger(frmSatuanBesar.class.getName()).log(Level.SEVERE, null, ex);
        }
   
    }
}
